﻿Public Class FABM_Tareas

    Dim sql As New Mysql

    'CARGAR EL COMBO
    Private Sub Cargar_combo(ByVal Combo As System.Object, ByVal Texto As String, ByVal Clave As String)
        Combo.Items.Add(New MiComboBox(Texto, Clave))
    End Sub


    Private Sub CargarGrillaTareas(ByVal ConsultaSQL As String)
        Dim Y
        Dim x

        GrillaHistorialTareas.Rows.Clear()

        Dt = sql.LEER(ConsultaSQL)

        For x = 0 To Dt.Rows.Count - 1
            GrillaHistorialTareas.Rows.Add(1)
            Y = GrillaHistorialTareas.Rows.Count - 2
            GrillaHistorialTareas.Item(0, Y).Value = Dt.Rows(x).Item("id_tarea")
            GrillaHistorialTareas.Item(1, Y).Value = Dt.Rows(x).Item("fecha_inicio")
            GrillaHistorialTareas.Item(2, Y).Value = Dt.Rows(x).Item("area")
            GrillaHistorialTareas.Item(3, Y).Value = Dt.Rows(x).Item("subarea")
            GrillaHistorialTareas.Item(4, Y).Value = Dt.Rows(x).Item("estado")
            GrillaHistorialTareas.Item(5, Y).Value = Dt.Rows(x).Item("Tarea_Titulo")
            GrillaHistorialTareas.Item(6, Y).Value = Dt.Rows(x).Item("NombreApellido")
            GrillaHistorialTareas.Item(7, Y).Value = Dt.Rows(x).Item("Observacion_final")
        Next

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCancelar.Click
        GBDatosGenerales.Enabled = False
        GBTareas.Enabled = False
        btGuardar.Enabled = False
        btCancelar.Enabled = False
        btNuevo.Enabled = True
    End Sub

    Private Sub btNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btNuevo.Click

        GBDatosGenerales.Enabled = True
        GBTareas.Enabled = True
        btGuardar.Enabled = True
        btCancelar.Enabled = True
        btNuevo.Enabled = False
    End Sub



    Private Sub btGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGuardar.Click
        Dim response As MsgBoxResult
        Dim id_subarea_t
        Dim id_estado_t
        Dim id_area_t
        Dim id_usuario_t


        If CBArea.Text = "" Then
            id_subarea_t = -1
        Else
            id_subarea_t = CBSubarea.Items(CBSubarea.SelectedIndex).itemdata()
        End If

        If CBSubarea.Text = "" Then
            id_subarea_t = -1
        Else
            id_subarea_t = CBSubarea.Items(CBSubarea.SelectedIndex).itemdata()
        End If

        If CBArea.Text = "" Then
            id_area_t = -1
        Else
            id_area_t = CBArea.Items(CBArea.SelectedIndex).itemdata()
        End If

        If CBEstado.Text = "" Then
            id_estado_t = -1
        Else
            id_estado_t = CBEstado.Items(CBEstado.SelectedIndex).itemdata()
        End If

        If CBEmpeado.Text = "" Then
            id_usuario_t = -1
        Else
            id_usuario_t = CBEmpeado.Items(CBEmpeado.SelectedIndex).itemdata()
        End If

        response = MsgBox("Esta Seguro que desea Guardar la Tarea", MsgBoxStyle.YesNo)
        If response = MsgBoxResult.Yes Then

            If Principal Then
                sql.INSERT_UPDATE_DELETE("update tareas t set t.id_area = '" & id_area_t & "', t.id_estado = '" & id_estado_t & "', t.tarea = '" & TxtTarea.Text & "', t.tarea_titulo = '" & txtTituloTarea.Text & "', t.id_subarea = '" & id_subarea_t & "', t.id_usuario = '" & id_usuario_t & "' where t.id_tarea = '" & Val(tarea) & "' ")
            Else
                sql.INSERT_UPDATE_DELETE("Insert into tareas (id_usuario_creador ,fecha_inicio ,id_area , id_estado, id_subarea, tarea, tarea_titulo, baja, id_usuario) values('" & id_usuario & "','" & txtFechaInicio.Text & "','" & id_area & "','" & id_estado & "','" & id_subarea & "', '" & TxtTarea.Text & "', '" & txtTituloTarea.Text & "', 'N', '-1')")
            End If

            GBDatosGenerales.Enabled = False
            GBTareas.Enabled = False
            btGuardar.Enabled = False
            btCancelar.Enabled = False
            btNuevo.Enabled = True
            Me.Close()
        End If



    End Sub

    Private Sub btSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSalir.Click
        Me.Close()
    End Sub

    Private Sub FABM_Tareas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CargarGrillaTareas("select t.Observacion_final ,t.id_tarea, t.fecha_inicio, t.tarea_titulo, a.area, te.descripcion as estado, sa.subarea, u.NombreApellido from tareas t left join tareas_estados te on (t.id_estado = te.id_estado) left join areas a on (t.id_area = a.id_area) left join subarea sa on (t.id_subarea = sa.id_subarea) left join usuarios u on (t.id_usuario = u.Id_usuario) where (t.baja <> 'S') and (t.id_usuario_creador = '" & id_usuario & "')")

        GBDatosGenerales.Enabled = True
        GBTareas.Enabled = True

        If id_area <> 1 Then
            CBArea.Enabled = False
            CBEmpeado.Enabled = False
            CBEstado.Enabled = False
            CBSubarea.Enabled = False
        Else
            If id_subarea = 1 Then
                CBArea.Enabled = False
                CBEmpeado.Enabled = False
                CBEstado.Enabled = False
                CBSubarea.Enabled = True
            Else
                If id_actividad = 1 Then
                    CBArea.Enabled = False
                    CBEmpeado.Enabled = True
                    CBEstado.Enabled = False
                    CBSubarea.Enabled = False
                End If
            End If

        End If



        '-------Cargo Combo Area-----------------------------------------
        CBArea.Items.Clear()

        Dt = sql.LEER("select * from areas a where (a.baja <> 'S')")

        For i As Integer = 0 To Dt.Rows.Count - 1
            If Dt.Rows.Count = 0 Then
                MsgBox("NO HAY REGISTROS PARA CONSULTAR")
                Exit Sub
            Else
                Cargar_combo(CBArea, Dt.Rows(i).Item("area").ToString(), Dt.Rows(i).Item("id_area").ToString())
            End If
        Next i

        '-------Cargo Combo SubArea-----------------------------------------
        CBSubarea.Items.Clear()

        Dt = sql.LEER("select * from subarea a where (a.baja <> 'S')")

        For i As Integer = 0 To Dt.Rows.Count - 1
            If Dt.Rows.Count = 0 Then
                MsgBox("NO HAY REGISTROS PARA CONSULTAR")
                Exit Sub
            Else
                Cargar_combo(CBSubarea, Dt.Rows(i).Item("subarea").ToString(), Dt.Rows(i).Item("id_subarea").ToString())
            End If
        Next i


        '-------Cargo Combo Estado-----------------------------------------
        CBEstado.Items.Clear()

        Dt = sql.LEER("select * from tareas_estados a ")

        For i As Integer = 0 To Dt.Rows.Count - 1
            If Dt.Rows.Count = 0 Then
                MsgBox("NO HAY REGISTROS PARA CONSULTAR")
                Exit Sub
            Else
                Cargar_combo(CBEstado, Dt.Rows(i).Item("Descripcion").ToString(), Dt.Rows(i).Item("Id_estado").ToString())
            End If
        Next i

        '-------Cargo Combo Usuarios-----------------------------------------
        CBEmpeado.Items.Clear()

        Dt = sql.LEER("select * from usuarios a where (a.id_subarea = '" & id_subarea & "') and (a.id_subarea <> 1) and (a.id_actividad <> 1)")

        For i As Integer = 0 To Dt.Rows.Count - 1
            If Dt.Rows.Count = 0 Then
                'MsgBox("NO HAY REGISTROS PARA CONSULTAR")
                Exit Sub
            Else
                Cargar_combo(CBEmpeado, Dt.Rows(i).Item("NombreApellido").ToString(), Dt.Rows(i).Item("Id_usuario").ToString())
            End If
        Next i


        If Principal Then
            btGuardar.Enabled = True
            btCancelar.Enabled = True
            btNuevo.Enabled = False

            Dt = sql.LEER("select t.*, u.NombreApellido, sa.subarea, a.area, te.descripcion as estado from tareas t left join tareas_estados te on (t.id_estado = te.id_estado) left join areas a on (t.id_area = a.id_area) left join subarea sa on (t.id_subarea = sa.id_subarea) left join usuarios u on (t.id_usuario = u.id_usuario) where (t.id_tarea = '" & Val(tarea) & "')")

            If Dt.Rows.Count <> 0 Then
                If Dt.Rows(0).Item("estado") Is DBNull.Value Then
                    CBEstado.Text = ""
                Else
                    CBEstado.Text = Dt.Rows(0).Item("estado")
                End If

                If Dt.Rows(0).Item("area") Is DBNull.Value Then
                    CBArea.Text = ""
                Else
                    CBArea.Text = Dt.Rows(0).Item("area")
                End If

                If Dt.Rows(0).Item("NombreApellido") Is DBNull.Value Then
                    CBEmpeado.Text = ""
                Else
                    CBEmpeado.Text = Dt.Rows(0).Item("NombreApellido")
                End If

                If Dt.Rows(0).Item("Subarea") Is DBNull.Value Then
                    CBSubarea.Text = ""
                Else
                    CBSubarea.Text = Dt.Rows(0).Item("Subarea")
                End If

                txtFechaInicio.Text = Format(Dt.Rows(0).Item("Fecha_inicio"), "yyyy/MM/dd")
                txtTituloTarea.Text = Dt.Rows(0).Item("Tarea_Titulo")
                TxtTarea.Text = Dt.Rows(0).Item("Tarea")
            End If

        Else
            GBDatosGenerales.Enabled = True
            GBTareas.Enabled = True
            btGuardar.Enabled = True
            btCancelar.Enabled = True
            btNuevo.Enabled = False

            Dt = sql.LEER("select * from usuarios u where u.id_usuario = '" & id_usuario & "' ")

            If Dt.Rows.Count <> 0 Then
                id_estado = 1

                If Dt.Rows(0).Item("id_area") Is DBNull.Value Then
                    id_area = -1
                Else
                    id_area = Dt.Rows(0).Item("id_area")
                End If

                If Dt.Rows(0).Item("id_actividad") Is DBNull.Value Then
                    id_actividad = -1
                Else
                    id_actividad = Dt.Rows(0).Item("id_actividad")
                End If

                id_subarea = -1
                txtFechaInicio.Text = Format(Now, "yyyy/MM/dd")
            End If
        End If


    End Sub

    Private Sub BtBaja_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class